package com.jjb.www.ui.activity;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.hjq.bar.TitleBar;
import com.hjq.base.BaseDialog;
import com.hjq.base.FragmentPagerAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jjb.www.R;
import com.jjb.www.app.AppActivity;
import com.jjb.www.app.AppFragment;
import com.jjb.www.bean.PostBean;
import com.jjb.www.http.api.CurriculumSortApi;
import com.jjb.www.http.api.PlanListApi;
import com.jjb.www.http.api.RegionsApi;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.other.AppConfig;
import com.jjb.www.ui.adapter.TabAdapter;
import com.jjb.www.ui.dialog.ChooseReasonDialog;
import com.jjb.www.ui.dialog.PlanListDialog;
import com.jjb.www.ui.fragment.CourseListFragment;
import com.jjb.www.ui.fragment.PreListFragment;
import com.jjb.www.ui.popup.CustomPartShadowPopupView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.SimpleCallback;

import java.util.ArrayList;
import java.util.List;

/**
 *    author : Android CRG
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 岗前新训报名
 */
public class PreEmploymentActivity extends AppActivity implements TabAdapter.OnTabListener, ViewPager.OnPageChangeListener {
    private RecyclerView mTabView;
    private ViewPager mViewPager;
    private TabAdapter mTabAdapter;
    private TitleBar mTitleBar;
    private CustomPartShadowPopupView popupView;
    private String leftName = "";
    private String rightName = "";
    private String seachName = "";
    List<PostBean> addresslist = new ArrayList<>();

    private List<PostBean> planList = new ArrayList<>();
    private Resources resources;
    //选中的市区
    private Integer mPersion = 0;
    //选中的岗位
    private Integer mTopPersion = 0;
    //选中的地址id
    private Integer addressId = 0;
    /**
     * 岗位id
     * 1	专业人员
     * 44	检测人员
     * 89	特种作业人员
     * 114	安管人员
     * 143	监理人员
     */
    private Integer pid = 1;
    /**
     * 1八大员 2检测人员 4特种作业人员 5安管人员 6监理人员
     */
    private String train_type = "1";
    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_pre_employment;
    }

    @Override
    protected void initView() {
        mTabView = findViewById(R.id.rv_pre_tab);
        mViewPager = findViewById(R.id.vp_pre_pager);
        mTitleBar = findViewById(R.id.titleBar);
        mPagerAdapter = new FragmentPagerAdapter<>(this);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(this);
        mTabAdapter = new TabAdapter(this, TabAdapter.TAB_MODE_DESIGN, false);

        mTabView.setAdapter(mTabAdapter);
        mTabAdapter.setOnTabListener(this);
        setOnClickListener(R.id.img_all);
    }
//        @Override
//    public Resources getResources() {
//        if (resources==null){
//            resources = super.getResources();
//            Configuration configuration = resources.getConfiguration();
//            configuration.densityDpi = (int) (configuration.densityDpi* AppConfig.getInstance().getDensityDpi());
//            resources.updateConfiguration(configuration,resources.getDisplayMetrics());
//        }
//        return resources;
//    }

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return super.isStatusBarEnabled();
    }
    @Override
    protected void initData() {
        PostBean postBean1 = new PostBean();
        postBean1.setName("专业人员");
        postBean1.setPid(1);
        planList.add(postBean1);

        PostBean postBean2 = new PostBean();
        postBean2.setPid(89);
        postBean2.setName("特种作业人员");
        planList.add(postBean2);

        PostBean postBean3 = new PostBean();
        postBean3.setName("检测人员");
        postBean3.setPid(44);
        planList.add(postBean3);

        PostBean postBean4 = new PostBean();
        postBean4.setPid(143);
        postBean4.setName("监理人员");
        planList.add(postBean4);

        PostBean postBean5 = new PostBean();
        postBean5.setName("安管人员");
        postBean5.setPid(114);
        planList.add(postBean5);
        getAddress();
        getPlanList(pid);
    }

    /**
     * 获取所有地址
     */
    private void getAddress() {
        EasyHttp.get(this)
                .api(new RegionsApi()
                )
                .request(new HttpCallback<HttpList<RegionsApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpList<RegionsApi.Bean> data) {

                        for (RegionsApi.Bean bean :data.getData()) {
                            PostBean postBean = new PostBean();
                            postBean.setName(bean.getName());
                            postBean.setPid(bean.getId());
                            addresslist.add(postBean);
                        }

                    }

                });
    }

    /**
     * 根据专业获取岗位
     */
    private void getPlanList(Integer mPid) {
        EasyHttp.get(this)
                .api(new PlanListApi()
                .setPid(mPid+""))
                .request(new HttpCallback<HttpList<PlanListApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpList<PlanListApi.Bean> data) {
                        mTabAdapter.clearData();
                        mPagerAdapter.clearFragment();
                        mTabAdapter.addItem("全部");
                        mViewPager.setAdapter(mPagerAdapter);

                        mPagerAdapter.addFragment(PreListFragment.newInstance(train_type,"0",addressId+""), "全部");
                        for (PlanListApi.Bean bean:data.getData()) {
                            mTabAdapter.addItem(bean.getNew_name());
                            mPagerAdapter.addFragment(PreListFragment.newInstance(train_type,bean.getId()+"",addressId+""), bean.getNew_name());
                        }

                        for (int i = 0; i < mTabAdapter.getData().size(); i++) {
                            if (rightName.equals(mTabAdapter.getData().get(i))){
                                mTabAdapter.setSelectedPosition(i);
                                mTabView.smoothScrollToPosition(i);
                                mViewPager.setCurrentItem(i);
                            }
                        }
                    }

                });
    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.img_all){
            Intent intent = new Intent();
            intent.putExtra("leftName",leftName);
            intent.putExtra("rightName",rightName);
            intent.setClass(this,SelectPostActivity.class);
            startActivityForResult(intent, new OnActivityCallback() {
                @Override
                public void onActivityResult(int resultCode, @Nullable Intent data) {
                    if (resultCode == RESULT_OK){
                        leftName = data.getStringExtra("leftName");
                        rightName = data.getStringExtra("rightName");
                        Log.e("rightName",leftName+"---"+rightName);
                        /**
                         * 岗位id
                         * 1	专业人员
                         * 44	检测人员
                         * 89	特种作业人员
                         * 114	安管人员
                         * 143	监理人员
                         */
                        for (int i = 0; i < planList.size(); i++) {

                            if (planList.get(i).getName().equals(leftName)){
                                mTitleBar.setTitle(leftName);
                                pid = planList.get(i).getPid();
                                /**
                                 * 1八大员 2检测人员 4特种作业人员 5安管人员 6监理人员
                                 */
                                if (pid==1){
                                    train_type = "1";
                                }else if (pid==44){
                                    train_type = "2";
                                }else if (pid==89){
                                    train_type = "4";
                                }else if (pid==114){
                                    train_type = "5";
                                }else if (pid==143){
                                    train_type = "6";
                                }
                                getPlanList(pid);
                            }
                        }
                    }
                }
            });
        }
    }

    @Override
    public void onRightClick(View view) {
        new ChooseReasonDialog.Builder(getContext())
                .setList(addresslist,mPersion)
                .setTitle("请选择所在地区")
                .setListener(new ChooseReasonDialog.OnListener() {
                    @Override
                    public void onCompleted(BaseDialog dialog, PostBean content, Integer persion) {
                        mPersion = persion;
                        mTitleBar.setRightTitle(content.getName());
                        addressId = content.getPid();
                        getPlanList(pid);
                    }
                }).show();
    }

    @Override
    public void onTitleClick(View view) {
        new ChooseReasonDialog.Builder(getContext())
                .setList(planList,mTopPersion)
                .setListener(new ChooseReasonDialog.OnListener() {
                    @Override
                    public void onCompleted(BaseDialog dialog, PostBean content, Integer persion) {
                        mTopPersion = persion;
                        mTitleBar.setTitle(content.getName());
                        pid = content.getPid();
                        if (pid==1){
                            train_type = "1";
                        }else if (pid==44){
                            train_type = "2";
                        }else if (pid==89){
                            train_type = "4";
                        }else if (pid==114){
                            train_type = "5";
                        }else if (pid==143){
                            train_type = "6";
                        }
                        getPlanList(content.getPid());
                    }
                }).show();
//        popupView = (CustomPartShadowPopupView) new XPopup.Builder(getContext())
//                .atView(mTitleBar)
//                .autoOpenSoftInput(false)
//                .setPopupCallback(new SimpleCallback() {
//                    @Override
//                    public void onShow(BasePopupView popupView) {
//                    }
//                    @Override
//                    public void onDismiss(BasePopupView popupView) {
//
//                    }
//                })
//                .asCustom(new CustomPartShadowPopupView(getContext(), list,mTopPersion ,new CustomPartShadowPopupView.OnListener() {
//                    @Override
//                    public void onCompleted(String content, Integer position) {
//                        mTopPersion = position;
//                        mTitleBar.setTitle(content);
//                    }
//                }));
//
//        popupView.show();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (mTabAdapter == null) {
            return;
        }
        mTabView.smoothScrollToPosition(position);
        mTabAdapter.setSelectedPosition(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onTabSelected(RecyclerView recyclerView, int position) {
        mViewPager.setCurrentItem(position);
        return true;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mViewPager.setAdapter(null);
        mViewPager.removeOnPageChangeListener(this);
        mTabAdapter.setOnTabListener(null);
    }
}